<?php

namespace App\Service\Message;

use App\Cache\UserRemarkCache;
use App\Model\CallBase;
use App\Model\Message;
use App\Model\Service\CacheService;
use App\Model\User;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Service\BaseService;

/**
 * 聊友列表信息
 */
class ChatUsersService extends BaseService
{
    protected function RoVerifyList()
    {
        return [
            'im_accounts' => 'required|string'
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $this->userId = $userId;
        $imAccounts = $this->request->input('im_accounts', '');
        $imAccounts = explode(',', $imAccounts);
        $imAccounts = array_slice($imAccounts, 0, 100);
        $userIds = User::getUserIdByIm($imAccounts, 'im_kv');
        $users = UserProfile::getUsersProfile(array_keys($userIds));
        $ret['list'] = [];
        array_walk($users, function ($user) use (&$ret, $userIds) {
            $item = UserProfile::buildUserItem($user, $userIds);

            $item['remark'] = $this->userId != $user['user_id'] ? (new UserRemarkCache())->getRemark($this->userId, $user['user_id']) : '';

            $ret['list'][] = $item;
        });
        //自动消息设置的浮窗
        $ret['auto_msg_setting'] = UserProfile::checkIsHost($userId) ? 1 : 0;
        //标签用户
        $this->packTagUsers($ret, $userId);
        return $ret;
    }

    /**
     * 标签用户筛选
     */
    function packTagUsers(&$ret = [], $userId = 0)
    {
        return;
        if (!User::checkValidVersion($this->curVersion, $this->appId, ['1.0.0'])) return;
        //视频过
        $videoCalled = CacheService::getCalledUsers($userId, VideoCall::CALL_TYPE);
        //语音过
        $voiceCalled = CacheService::getCalledUsers($userId, VoiceCall::CALL_TYPE);
        //互动
        $chatUsers = CacheService::getChatedUsers($userId);
        foreach ($ret['list'] as &$item) {
            $uid = $item['user_id'];
            $item['user_tag'] = [];
            if (in_array($uid, $videoCalled)) $item['user_tag'][] = Message::MSG_TAG[1]['tag_name'];
            elseif (in_array($uid, $voiceCalled)) $item['user_tag'][] = Message::MSG_TAG[3]['tag_name'];
            elseif (in_array($uid, $chatUsers)) $item['user_tag'][] = Message::MSG_TAG[2]['tag_name'];
        }
    }
}
